/*
 * This file is part of andExplorer, android.ovhoo.com
 * Copyright (C) 2007 Mohamed ZALIM <mzalim@ovhoo.com>
 *
 * andExplorer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * andExplorer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */
package com.ovhoo.android.fiveexplore;

import com.ovhoo.android.fiveexplore.R;
import com.ovhoo.android.file.CurentDirManager;
import com.ovhoo.android.file.FileInterface;

import com.ovhoo.android.file.MimeTypes;

import android.app.Dialog;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * CLass implements the dialog shown to the user after the click on a file
 * @author mzalim
 *
 */
public class OpenFileDialog extends AbstractOpenFileDialog implements OnClickListener{
	
	
	private static final int HANDLER_MSG_CONFIRM_RENAME = 0;
	private static final int HANDLER_MSG_CONFIRM_DELETE = 1;
	
	private ImageView fileIcon;
	
	private TextView fileBaseName, fileMimeType, fileSize, fileDir;
	private Button openFile, suppressFile, renameFile, cancelButton;
	
		
	public OpenFileDialog(Context context, CurentDirManager fileManager, FileInterface file) {
		super(context, fileManager, file);
		this.buildUI(); //Creation of the UI
		
	}
	
	protected void buildUI(){
		LayoutInflater _inflater=this.getLayoutInflater();
		View _elementView = _inflater.inflate(R.layout.openfile, null);
		
		this.fileIcon = (ImageView) _elementView.findViewById(R.id.OPEN_FILE_BIG_ICON);
		
		this.fileBaseName = (TextView) _elementView.findViewById(R.id.OPEN_FILE_BASENAME);
		this.fileMimeType = (TextView) _elementView.findViewById(R.id.OPEN_FILE_MIME_TYPE);
		this.fileSize = (TextView) _elementView.findViewById(R.id.OPEN_FILE_SIZE);
		this.fileDir = (TextView) _elementView.findViewById(R.id.OPEN_FILE_DIR);
		
		//Buttons
		this.openFile = (Button) _elementView.findViewById(R.id.OPEN_FILE_OPEN);
		this.renameFile = (Button) _elementView.findViewById(R.id.OPEN_FILE_RENAME);
		this.suppressFile = (Button) _elementView.findViewById(R.id.OPEN_FILE_DELETE);
		this.cancelButton = (Button) _elementView.findViewById(R.id.OPEN_FILE_CANCEL);
		
		//Listener
		this.openFile.setOnClickListener(this);
		this.renameFile.setOnClickListener(this);
		this.suppressFile.setOnClickListener(this);
		this.cancelButton.setOnClickListener(this);
		
		this.setContentView(_elementView);
	}
	
	public void show(){
		this.fileIcon.setImageResource(this.mimeTypes.getFileIcon(file));
		
		this.fileBaseName.setText(file.getName());
		this.fileMimeType.setText(mimeTypes.getMimeType(file));
		this.fileSize.setText(file.getFileSize());
		this.fileDir.setText(file.getParentDir());
		
		//Hide Open button for unknown files
		if (mimeTypes.getMimeType(file) == null){
			this.openFile.setEnabled(false);
		}else if(mimeTypes.getMimeType(file).length()==0){
			this.openFile.setEnabled(false);
		}
		else{
			this.openFile.setEnabled(true);
		}
		super.show();
	}



	@Override
	public void onClick(View v) {
		if (v == this.openFile){
			this.onOpenFile();
		}
		else if (v == this.renameFile){
			this.onRenameFile();
		}
		else if (v == this.suppressFile){
			this.onSuppressFile();
		}
		else if(v == this.cancelButton){
			this.dismiss();
		}
	}
	
	protected boolean onOpenFile(){
		try {
			Intent myIntent =new Intent(android.content.Intent.ACTION_VIEW);
			myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			myIntent.setDataAndType(file.getUri(), this.mimeTypes.getMimeType(file) );
			this.getContext().startActivity(myIntent);
			
			return true;
		} catch( ActivityNotFoundException e){
			this.showAlertMessage(this.getContext().getResources().getString(R.string.OPEN_FILE_CANNOT_OPEN));
			System.err.println("OpenFileDialog::onClick ActivityNotFoundException ");
			e.printStackTrace();
			return false;
		}
	}
	
	
	protected void onRenameFile(){
		RenameFileDialog _dialog = new RenameFileDialog(this.getContext(), this.handler.obtainMessage(HANDLER_MSG_CONFIRM_RENAME, null), this.file);
		_dialog.show();
	}
	
	protected void onSuppressFile(){
		DeleteFileDialog _dialog = new DeleteFileDialog(this.getContext(), this.handler.obtainMessage(HANDLER_MSG_CONFIRM_DELETE, null), this.file);
		_dialog.show();
	}
	
	/**
	 * Display an alert message
	 * @param msg the alert message to be displayed
	 */
	protected void showAlertMessage(String msg){
		
		this.alertDialog.setMessage(msg);
		this.alertDialog.show();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	Handler handler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			
			if (msg != null){
				switch(msg.what){
					case HANDLER_MSG_CONFIRM_RENAME : this.rename((String) msg.obj); break;
					case HANDLER_MSG_CONFIRM_DELETE : this.delete(); break;
				}
				
			}
			
		}
		
		public void rename( String baseName){
			if (fileManager.rename(file, baseName)){
				dismiss();
			}
			else{
				showAlertMessage(getContext().getResources().getString(R.string.OPEN_FILE_CANNOT_RENAME));
			}
			
		}
		
		public void delete(){
			if (fileManager.rm(file)){
				dismiss();
			}
			else{
				showAlertMessage(getContext().getResources().getString(R.string.OPEN_FILE_CANNOT_DELTE));
			}
		}
	};
}
